package com.creatapark.loader;

import com.creatapark.entity.CarData;
import com.creatapark.mapper.ParkInOutMapper;
import com.creatapark.util.PlateNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * ParkDataLoader
 *
 * @author Bruce01.fan
 * @date 2017/12/16
 */
@Component
public class ParkDataLoader {

    private int buffer = 1000;
    private int price = 16;
    private int avgHour = 1 * 60 * 60 * 1000;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    DecimalFormat df=new DecimalFormat("0.00");

    @Autowired
    ParkInOutMapper parkInOutMapper;

    //@PostConstruct
    public void dataLoading()throws Exception {

        System.out.println("Starting Car Data Loading ....");

        List<CarData> carDataList_day1 = this.loadingData("2017-12-08 23:00","2017-12-09 23:59",Arrays.asList(596 ,594 ,591 ,589 ,587 ,584 ,556 ,500 ,450 ,562 ,801 ,881 ,953 ,966 ,890 ,870 ,920 ,930 ,989 ,989 ,980 ,950 ,760 ,722));
        List<CarData> carDataList_day2 = this.loadingData("2017-12-09 23:00","2017-12-10 23:59",Arrays.asList(610 ,608 ,605 ,603 ,600 ,598 ,539 ,323 ,291 ,407 ,489 ,513 ,590 ,531 ,292 ,307 ,322 ,370 ,593 ,770 ,809 ,728 ,655 ,622));

        carDataList_day1.forEach(carData -> parkInOutMapper.insertData(carData));
        carDataList_day2.forEach(carData -> parkInOutMapper.insertData(carData));

    }

    public List<CarData> loadingData(String start,String end,List<Integer> carFlowList) throws ParseException {

        List<CarData> carDataList = new ArrayList<>();

        Map<Integer,Integer> carFlow = new HashMap<>();
        for(int i=0;i<24;i++) {
            carFlow.put(i,carFlowList.get(i));
        }

        long startTime = simpleDateFormat.parse(start).getTime();
        long endTime = simpleDateFormat.parse(end).getTime();


        carFlow.forEach((hour,size)->{
            int tmp = 0;

            System.out.println(carDataList.size()+" =====> ");
            while(tmp <size) {
                long newStartTime = getDate(startTime,endTime);
                String newStartStr = simpleDateFormat.format(new Date(newStartTime));

                Date endDate = new Date(newStartTime + avgHour);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(endDate);
                int h = calendar.get(Calendar.HOUR_OF_DAY);
                String newEndStr = simpleDateFormat.format(endDate);

                if(hour == h) {

                    CarData carData = new CarData();

                    carData.setParkId(1L);
                    carData.setIntTime(newStartStr);
                    carData.setOutTime(newEndStr);
                    carData.setPlateNumber(PlateNumberGenerator.generate());

                    carDataList.add(carData);

                    tmp++;
                }

                if(tmp  == size) {
                    System.out.println("hour:"+hour+",size:"+size+",tmp:"+tmp);
                }

            }

        });

        return  carDataList;
    }

    public void calculate(List<CarData> carDatas) {
        int flow = carDatas.size();
        String percenge = df.format((double)flow / buffer);
        int revenue = flow * price;

        System.out.println("车流量:"+flow+",利用率:"+percenge+",营业额:"+revenue);

    }

    public long getDate(long start,long end) {

        long hour = (long) (start+(end-start)* (Math.random()));

        if(hour < start || (hour + avgHour) > end) {
            getDate(start,end);
        }

        return hour;

    }


    public static void main(String[] args) throws ParseException {

        ParkDataLoader parkDataLoader = new ParkDataLoader();

        String start = "2017-12-00 23:00";
        String end = "2017-12-01 23:59";

        List<CarData> carDataList = parkDataLoader.loadingData(start,end,Arrays.asList(635 ,632 ,630 ,627 ,625 ,622 ,561 ,336 ,303 ,424 ,530 ,556 ,668 ,601 ,361 ,379 ,398 ,477 ,730 ,876 ,920 ,828 ,745 ,708));
        parkDataLoader.calculate(carDataList);
    }

}
